package com.maidou.qiantai.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Collections;

/**
 * @Author maidou
 * @Date 2023/11/23 21:36
 * 跨域
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1,允许任何来源
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
        //2,允许任何请求头
        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
        //2,允许任何响应头
        /*暴露哪些头部信息 不能用*因为跨域访问默认不能获取全部头部信息*/
//        corsConfiguration.addExposedHeader("token");
        //3,允许任何方法
        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
        //4,允许凭证
        corsConfiguration.setAllowCredentials(true);
        //允许暴露的响应头
        /*暴露哪些头部信息 不能用*因为跨域访问默认不能获取全部头部信息*/
//        corsConfiguration.addExposedHeader(HeritageSiteConstant.JwtEnum.ACCESS_TOKEN.getValue());
//        corsConfiguration.addExposedHeader(HeritageSiteConstant.JwtEnum.REFRESH_TOKEN.getValue());

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }

}
